Eth2 Phase 1: SHARD BLOCK OFFSETS
WIP
The type of shard_block_lengths is List[uint64, MAX_SHARD_BLOCKS_PER_ATTESTATION].
MAX_SHARD_BLOCKS_PER_ATTESTATION is len(SHARD_BLOCK_OFFSETS), and SHARD_BLOCK_OFFSETS is List[uint64, 12]([1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233]).
This appers to be Fibonacci sequence. #? Related functions
code:py
def compute_offset_slots(start_slot: Slot, end_slot: Slot) -> SequenceSlot: """
Return the offset slots that are greater than start_slot and less than end_slot.
"""
code:py
def get_offset_slots(state: BeaconState, shard: Shard) -> SequenceSlot: """
Return the offset slots of the given shard.
The offset slot are after the latest slot and before current slot.
"""
return compute_offset_slots(get_latest_slot_for_shard(state, shard), state.slot)
apply_shard_transition
code:py
def apply_shard_transition(state: BeaconState, shard: Shard, transition: ShardTransition) -> None:
# TODO: only need to check it once when phase 1 starts
assert state.slot > PHASE_1_FORK_SLOT
# Correct data root count
offset_slots = get_offset_slots(state, shard)
assert (
len(transition.shard_data_roots)
== len(transition.shard_states)
== len(transition.shard_block_lengths)
== len(offset_slots)
)
assert transition.start_slot == offset_slots0 headers = []
proposers = []
prev_gasprice = state.shard_statesshard.gasprice shard_parent_root = state.shard_statesshard.latest_block_root for i, offset_slot in enumerate(offset_slots):
shard_block_length = transition.shard_block_lengthsi shard_state = transition.shard_statesi # Verify correct calculation of gas prices and slots
assert shard_state.gasprice == compute_updated_gasprice(prev_gasprice, shard_block_length)
assert shard_state.slot == offset_slot
# Collect the non-empty proposals result
is_empty_proposal = shard_block_length == 0
if not is_empty_proposal:
proposal_index = get_shard_proposer_index(state, offset_slot, shard)
# Reconstruct shard headers
header = ShardBlockHeader(
shard_parent_root=shard_parent_root,
beacon_parent_root=get_block_root_at_slot(state, offset_slot),
slot=offset_slot,
shard=shard,
proposer_index=proposal_index,
body_root=transition.shard_data_rootsi )
shard_parent_root = hash_tree_root(header)
headers.append(header)
proposers.append(proposal_index)
else:
# Must have a stub for shard_data_root if empty slot
assert transition.shard_data_rootsi == Root() prev_gasprice = shard_state.gasprice
pubkeys = [state.validatorsproposer.pubkey for proposer in proposers] signing_roots = [
compute_signing_root(header, get_domain(state, DOMAIN_SHARD_PROPOSAL, compute_epoch_at_slot(header.slot)))
for header in headers
]
# Verify combined proposer signature
assert optional_aggregate_verify(pubkeys, signing_roots, transition.proposer_signature_aggregate)
# Copy and save updated shard state
shard_state.slot = compute_previous_slot(state.slot)
state.shard_statesshard = shard_state